package mh.map;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Transparency;
import java.awt.color.ColorSpace;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.ComponentColorModel;
import java.awt.image.DataBuffer;
import java.awt.image.DataBufferByte;
import java.awt.image.Raster;
import java.awt.image.WritableRaster;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.net.URL;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.IntBuffer;
import java.util.HashMap;
import java.util.Hashtable;

import javax.imageio.ImageIO;

import org.lwjgl.opengl.GL11;

/**
 * A utility class to load textures for JOGL. This source is based on a texture that can be found in the Java Gaming
 * (www.javagaming.org) Wiki. It has been simplified slightly for explicit 2D graphics use.<br>
 * <br>
 * OpenGL uses a particular image format. Since the images that are loaded from disk may not match this format this
 * loader introduces a intermediate image which the source image is copied into. In turn, this image is used as source
 * for the OpenGL texture.<br>
 * <br>
 * The original version of this class was from K.Glass and B.Matzon. There have been many changes that the main code is
 * almost gone, what remains is the idea and few implementation.<br>
 * 
 * @author Kevin Glass
 * @author Brian Matzon
 * @author Gianmarco Laggia
 */
public class TextureLoader {
	/** The table of textures that have been loaded in this loader */
	private static final HashMap<String, Texture>	table	= new HashMap<String, Texture>();

	/** The colour model including alpha for the GL image */
	private static ColorModel						glAlphaColorModel;

	/** The colour model for the GL image */
	private static ColorModel						glColorModel;

	/**
	 * Create a new texture loader based on the game panel
	 */
	public TextureLoader() {
		glAlphaColorModel = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB), new int[] { 8, 8, 8, 8 }, true, false,
				Transparency.TRANSLUCENT, DataBuffer.TYPE_BYTE);

		glColorModel = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB), new int[] { 8, 8, 8, 0 }, false, false, Transparency.OPAQUE,
				DataBuffer.TYPE_BYTE);
	}

	/**
	 * Create a new texture ID
	 * 
	 * @return A new texture ID
	 */
	private static int createTextureID() {
		IntBuffer tmp = createIntBuffer(1);
		GL11.glGenTextures(tmp);
		return tmp.get(0);
	}

	/**
	 * Load a texture
	 * 
	 * @param resourceName
	 *            The location of the resource to load
	 * @param cornerTrans
	 *            If the transparency should be taken from the top left pixel
	 * @param transCol
	 *            The color to use as transparent color.
	 * @return The loaded texture
	 * @throws IOException
	 *             Indicates a failure to access the resource
	 */
	public static Texture getTexture(String resourceName, boolean cornerTrans, Color transCol) throws IOException {
		Texture tex = table.get(resourceName);

		if (tex != null) {
			return tex;
		}

		tex = getTexture(resourceName, GL11.GL_TEXTURE_2D, // target
				GL11.GL_RGBA, // dst pixel format
				GL11.GL_LINEAR, // min filter (unused)
				GL11.GL_LINEAR, cornerTrans, transCol);

		table.put(resourceName, tex);

		return tex;
	}

	/**
	 * Load a texture
	 * 
	 * @param image
	 *            The image to load as texture
	 * @param cornerTrans
	 *            If the transparency should be taken from the top left pixel
	 * @param transCol
	 *            The color to use as transparent color.
	 * @return The loaded texture
	 * @throws IOException
	 *             Indicates a failure to access the resource
	 */
	public static Texture getTexture(BufferedImage image, boolean cornerTrans, Color transCol) throws IOException {
		Texture tex = getTexture(image, GL11.GL_TEXTURE_2D, // target
				GL11.GL_RGBA, // dst pixel format
				GL11.GL_LINEAR, // min filter (unused)
				GL11.GL_LINEAR, cornerTrans, transCol);
		return tex;
	}

	/**
	 * Load a texture into OpenGL from a image reference on disk.
	 * 
	 * @param resourceName
	 *            The location of the resource to load
	 * @param target
	 *            The GL target to load the texture against
	 * @param dstPixelFormat
	 *            The pixel format of the screen
	 * @param minFilter
	 *            The minimising filter
	 * @param magFilter
	 *            The magnification filter
	 * @param cornerTransparency
	 *            If true the method will take the top-left pixel's colour and make it transparent in the image
	 * @param transCol
	 *            If <code>cornerTransparency</code> is false, this is the color that will be set to transparent.
	 * @return The loaded texture
	 * @throws IOException
	 *             Indicates a failure to access the resource
	 */
	public static Texture getTexture(String resourceName, int target, int dstPixelFormat, int minFilter, int magFilter, boolean cornerTransparency, Color transCol) throws IOException {

		BufferedImage bufferedImage = loadImage(resourceName, cornerTransparency, transCol);
		return getTexture(bufferedImage, target, dstPixelFormat, minFilter, magFilter, cornerTransparency, transCol);
	}

	/**
	 * Load a texture into OpenGL from a image reference on disk.
	 * 
	 * @param bufferedImage
	 *            The Image to load as Texture
	 * @param target
	 *            The GL target to load the texture against
	 * @param dstPixelFormat
	 *            The pixel format of the screen
	 * @param minFilter
	 *            The minimising filter
	 * @param magFilter
	 *            The magnification filter
	 * @param cornerTransparency
	 *            If true the method will take the top-left pixel's colour and make it transparent in the image
	 * @param transCol
	 *            If <code>cornerTransparency</code> is false, this is the color that will be set to transparent.
	 * @return The loaded texture
	 * @throws IOException
	 *             Indicates a failure to access the resource
	 */
	public static Texture getTexture(BufferedImage bufferedImage, int target, int dstPixelFormat, int minFilter, int magFilter, boolean cornerTransparency, Color transCol) throws IOException {
		int srcPixelFormat = 0;

		int firstPixel = bufferedImage.getRGB(0, 0);
		BufferedImage bff = new BufferedImage(bufferedImage.getWidth(), bufferedImage.getHeight(), BufferedImage.TYPE_4BYTE_ABGR);
		for (int y = 0; y < bufferedImage.getHeight(); ++y) {
			for (int x = 0; x < bufferedImage.getWidth(); ++x) {
				int argb = bufferedImage.getRGB(x, y);
				if (cornerTransparency) {
					if (argb == firstPixel) {
						bff.setRGB(x, y, 0); //black with alpha = 0
					} else {
						bff.setRGB(x, y, argb);
					}
				} else {
					if (transCol != null) {
						if ((argb & 0xFF000000) == (transCol.getRGB() & 0xFF000000)) {
							bff.setRGB(x, y, 0); //black with alpha = 0
						} else {
							bff.setRGB(x, y, argb);
						}
					} else {
						if (argb == 0xFFFF00FF) {
							bff.setRGB(x, y, 0);
						} else {
							bff.setRGB(x, y, argb);
						}
					}
				}
			}
		}

		bufferedImage = bff;

		// create the texture ID for this texture
		int textureID = createTextureID();
		Texture texture = new Texture(target, textureID);

		// bind this texture
		GL11.glBindTexture(target, textureID);

		texture.setWidth(bufferedImage.getWidth());
		texture.setHeight(bufferedImage.getHeight());

		if (bufferedImage.getColorModel().hasAlpha()) {
			srcPixelFormat = GL11.GL_RGBA;
		} else {
			srcPixelFormat = GL11.GL_RGB;
		}

		// convert that image into a byte buffer of texture data
		ByteBuffer textureBuffer = convertImageData(bufferedImage, texture);

		if (target == GL11.GL_TEXTURE_2D) {
			GL11.glTexParameteri(target, GL11.GL_TEXTURE_MIN_FILTER, minFilter);
			GL11.glTexParameteri(target, GL11.GL_TEXTURE_MAG_FILTER, magFilter);
		}

		// produce a texture from the byte buffer
		GL11.glTexImage2D(target, 0, dstPixelFormat, get2Fold(bufferedImage.getWidth()), get2Fold(bufferedImage.getHeight()), 0, srcPixelFormat,
				GL11.GL_UNSIGNED_BYTE, textureBuffer);

		return texture;
	}

	/**
	 * Get the closest greater power of 2 to the fold number
	 * 
	 * @param fold
	 *            The target number
	 * @return The power of 2
	 */
	private static int get2Fold(int fold) {
		int ret = 2;
		while (ret < fold) {
			ret *= 2;
		}
		return ret;
	}

	/**
	 * Convert the buffered image to a texture
	 * 
	 * @param bufferedImage
	 *            The image to convert to a texture
	 * @param texture
	 *            The texture to store the data into
	 * @return A buffer containing the data
	 */
	private static ByteBuffer convertImageData(BufferedImage bufferedImage, Texture texture) {
		ByteBuffer imageBuffer = null;
		WritableRaster raster;
		BufferedImage texImage;

		int texWidth = 2;
		int texHeight = 2;

		// find the closest power of 2 for the width and height
		// of the produced texture
		while (texWidth < bufferedImage.getWidth()) {
			texWidth *= 2;
		}
		while (texHeight < bufferedImage.getHeight()) {
			texHeight *= 2;
		}

		texture.setTextureHeight(texHeight);
		texture.setTextureWidth(texWidth);

		// create a raster that can be used by OpenGL as a source
		// for a texture
		if (bufferedImage.getColorModel().hasAlpha()) {
			raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, texWidth, texHeight, 4, null);
			texImage = new BufferedImage(glAlphaColorModel, raster, false, new Hashtable<String, Texture>());
		} else {
			raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, texWidth, texHeight, 3, null);
			texImage = new BufferedImage(glColorModel, raster, false, new Hashtable<String, Texture>());
		}

		// copy the source image into the produced image
		Graphics g = texImage.getGraphics();
		g.setColor(new Color(0f, 0f, 0f, 0f));
		g.fillRect(0, 0, texWidth, texHeight);
		g.drawImage(bufferedImage, 0, 0, null);

		// build a byte buffer from the temporary image
		// that be used by OpenGL to produce a texture.
		byte[] data = ((DataBufferByte) texImage.getRaster().getDataBuffer()).getData();

		imageBuffer = ByteBuffer.allocateDirect(data.length);
		imageBuffer.order(ByteOrder.nativeOrder());
		imageBuffer.put(data, 0, data.length);
		imageBuffer.flip();

		return imageBuffer;
	}

	/**
	 * Load a given resource as a buffered image
	 * 
	 * @param ref
	 *            The location of the resource to load
	 * @param cornerTransparency
	 *            If true the method will take the top-left pixel's colour and make it transparent in the image
	 * @param transCol
	 *            If <code>cornerTransparency</code> is false, this is the color that will be set to transparent.
	 * @return The loaded buffered image
	 * @throws IOException
	 *             Indicates a failure to find a resource
	 */
	private static BufferedImage loadImage(String ref, boolean cornerTransparency, Color transCol) throws IOException {
		URL url = TextureLoader.class.getClassLoader().getResource(ref);
		if (url == null) {
			url = new URL("file:" + ref);
			//			throw new IOException("Cannot find: " + ref);
		}
		BufferedImage bufferedImage = ImageIO.read(new BufferedInputStream(url.openStream()));//getClass().getClassLoader().getResourceAsStream(ref)));

		return bufferedImage;
		//		int firstPixel = bufferedImage.getRGB(0, 0);
		//		if (bufferedImage.getType() == BufferedImage.TYPE_3BYTE_BGR) {
		//			if ((firstPixel & 0xFF000000) == 0xFF000000) {
		//				BufferedImage bff = new BufferedImage(bufferedImage.getWidth(), bufferedImage.getHeight(), BufferedImage.TYPE_4BYTE_ABGR);
		//				for (int y = 0; y < bufferedImage.getHeight(); ++y) {
		//					for (int x = 0; x < bufferedImage.getWidth(); ++x) {
		//						int argb = bufferedImage.getRGB(x, y);
		//						if (cornerTransparency) {
		//							if (argb == firstPixel) {
		//								bff.setRGB(x, y, 0); //black with alpha = 0
		//							} else {
		//								bff.setRGB(x, y, argb);
		//							}
		//						} else {
		//							if (transCol != null) {
		//								if ((argb & 0xFF000000) == (transCol.getRGB() & 0xFF000000)) {
		//									bff.setRGB(x, y, 0); //black with alpha = 0
		//								} else {
		//									bff.setRGB(x, y, argb);
		//								}
		//							} else {
		//								if ((argb & 0xFF000000) == 0xFFFF00FF) {
		//									bff.setRGB(x, y, 0);
		//								} else {
		//									bff.setRGB(x, y, argb);
		//								}
		//							}
		//						}
		//					}
		//				}
		//				return bff;
		//			} else {
		//				return bufferedImage;
		//			}
		//		} else {
		//			return bufferedImage;
		//		}

	}

	/**
	 * Creates an integer buffer to hold specified ints - strictly a utility method
	 * 
	 * @param size
	 *            how many int to contain
	 * @return created IntBuffer
	 */
	protected static IntBuffer createIntBuffer(int size) {
		ByteBuffer temp = ByteBuffer.allocateDirect(4 * size);
		temp.order(ByteOrder.nativeOrder());

		return temp.asIntBuffer();
	}

	//	/**
	//	 * Formats an URL to match the standard.
	//	 * 
	//	 * @param filename
	//	 *            The filename to format
	//	 * @return The filename formatted
	//	 * @throws MalformedURLException
	//	 */
	//	private static String makeUrl(String filename) throws MalformedURLException {
	//		final String url;
	//		if ((filename.indexOf("://") > 0) || filename.startsWith("file:")) {
	//			url = filename;
	//		} else {
	//			url = new File(filename).toURI().toString();
	//		}
	//		return url;
	//	}

}
